package cn.iocoder.yudao.module.famous.doctor.service.forumRecord;

import cn.iocoder.yudao.module.famous.doctor.controller.admin.reply.Enum.ReplyEnum;
import cn.iocoder.yudao.module.famous.doctor.dal.dataobject.reply.FamReplyDO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;

import java.util.*;
import java.util.stream.Collectors;

import cn.iocoder.yudao.module.famous.doctor.controller.admin.forumRecord.vo.*;
import cn.iocoder.yudao.module.famous.doctor.dal.dataobject.forumRecord.FamForumRecordDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.famous.doctor.convert.forumRecord.FamForumRecordConvert;
import cn.iocoder.yudao.module.famous.doctor.dal.mysql.forumRecord.FamForumRecordMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUser;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
import static cn.iocoder.yudao.module.famous.doctor.enums.ErrorCodeConstants.*;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;

/**
 * 点赞/收藏--记录表 Service 实现类
 *
 * @author 档案
 */
@Service
@Validated
public class FamForumRecordServiceImpl implements FamForumRecordService {

    @Autowired
    RedisTemplate redisTemplate;

    @Resource
    private FamForumRecordMapper famForumRecordMapper;

    @Override
    public Long createFamForumRecord(FamForumRecordCreateReqVO createReqVO) {
        // 插入
        FamForumRecordDO famForumRecord = FamForumRecordConvert.INSTANCE.convert(createReqVO);
        famForumRecordMapper.insert(famForumRecord);
        // 返回
        return famForumRecord.getId();
    }

    /**
     * hashkey不存在则添加redis   存在查询数据库是否存在存在需要删除数据库数据  然后删除redis
     * @param
     * @return
     */
    @Override
    public Long createRedis(FamForumRecordCreateReqVO createRedisReqVO) {
        FamForumRecordDO createReqVO = FamForumRecordConvert.INSTANCE.convert(createRedisReqVO);
        createReqVO.setCreator(getLoginUserId()+"");
        String key = "fam_forum_record" + ":" + createReqVO.getState() ;
        String hashkey = getLoginUserId() + "_" + createReqVO.getForumId() + "_" + createReqVO.getThumbsupId();
        //点赞/收藏 记录
        Boolean famForumRecord = redisTemplate.opsForHash().putIfAbsent(key, hashkey, createReqVO);
        System.out.println("状态：" + famForumRecord);
        if (!famForumRecord) {
            FamForumRecordDO famForumRecordDO = (FamForumRecordDO) redisTemplate.opsForHash().get(key, hashkey);
            if (famForumRecordDO.getId() != null) {
                // 数据库删除
                famForumRecordMapper.deleteById(famForumRecordDO.getId());
            }
            redisTemplate.opsForHash().delete(key, hashkey);
            return 0L;
        }
        return 1L;
    }

    @Override
    public List<FamForumRecordDO> getMyRedis(Long mystate) {
        String key = "fam_forum_record" + ":" + mystate ;
        List<FamForumRecordDO> famForumRecordDOList = redisTemplate.opsForHash().values(key);
        Long loginUserId = getLoginUserId();
        List<FamForumRecordDO> collect = famForumRecordDOList.stream()
                .filter(famForumRecordDO -> famForumRecordDO.getCreator().trim().equals(loginUserId+"".trim()))
                .collect(Collectors.toList());
        return collect;
    }

    @Override
    public void updateMyRedis(FamForumRecordUpdateReqVO updateReqVO) {
        String key = "fam_forum_record" + ":" + updateReqVO.getState();
        String hashkey = getLoginUserId() + "_" + updateReqVO.getForumId() + "_" + updateReqVO.getThumbsupId();
        FamForumRecordDO famForumRecord = FamForumRecordConvert.INSTANCE.convert(updateReqVO);

        if (famForumRecord.getId()==null) {
            //添加
            famForumRecordMapper.insert(famForumRecord);
        }else {
            //修改
            famForumRecordMapper.updateById(famForumRecord);
        }
        //更新redis
        redisTemplate.opsForHash().putIfAbsent(key, hashkey, updateReqVO);
    }

    @Override
    public void updateFamForumRecord(FamForumRecordUpdateReqVO updateReqVO) {
        // 校验存在
        validateFamForumRecordExists(updateReqVO.getId());
        // 更新
        FamForumRecordDO updateObj = FamForumRecordConvert.INSTANCE.convert(updateReqVO);
        famForumRecordMapper.updateById(updateObj);
    }

    @Override
    public void deleteFamForumRecord(Long id) {
        // 校验存在
        validateFamForumRecordExists(id);
        // 删除
        famForumRecordMapper.deleteById(id);
    }

    private void validateFamForumRecordExists(Long id) {
        if (famForumRecordMapper.selectById(id) == null) {
            throw exception(FAM_FORUM_RECORD_NOT_EXISTS);
        }
    }

    @Override
    public FamForumRecordDO getFamForumRecord(Long id) {
        return famForumRecordMapper.selectById(id);
    }

    @Override
    public List<FamForumRecordDO> getFamForumRecordList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return famForumRecordMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<FamForumRecordDO> getFamForumRecordPage(FamForumRecordPageReqVO pageReqVO) {
        return famForumRecordMapper.selectPage(pageReqVO);
    }

    @Override
    public List<FamForumRecordDO> getFamForumRecordList(FamForumRecordExportReqVO exportReqVO) {
        return famForumRecordMapper.selectList(exportReqVO);
    }

    @Override
    public List<FamForumRecordDO> getFamForumRecordList2(FamForumRecordExportReqVO exportReqVO, Boolean b) {
        return famForumRecordMapper.selectList2(exportReqVO, b);
    }

}
